package com.egoo.ticket.server.service.impl;

import com.egoo.ticket.server.config.redis.RedisKeyConstant;
import com.egoo.ticket.server.dao.EwsUserDao;
import com.egoo.ticket.server.dao.SysDictDao;
import com.egoo.ticket.server.pojo.dto.NonSysDictParamDto;
import com.egoo.ticket.server.pojo.dto.SysDictChildrenDto;
import com.egoo.ticket.server.pojo.dto.SysDictDto;
import com.egoo.ticket.server.pojo.dto.SysDictParamSetDto;
import com.egoo.ticket.server.pojo.entity.SysDict;
import com.egoo.ticket.server.pojo.vo.SysDictVo;
import com.egoo.ticket.server.service.SysDictService;
import com.egoo.ticket.server.utils.common.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author：victor_tang
 * @Date ：2020/1/6
 * @Description：系统字典实现类
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class SysDictServiceImpl implements SysDictService {

	@Autowired
	private SysDictDao sysDictDao;

	@Autowired
	private EwsUserDao ewsUserDao;

	@Autowired
	RedisUtil redisUtil;
	@Autowired
	CreateCodeUtil createCodeUtil;
	/**
	 * TODO 根据字典编码查询字典详情列表
	 * @author: victor_tang
	 * @createtime: 2020/1/7 18:21
	 * @param dictCode 1
	 * @return: com.egoo.ews.service.utils.DBResponse
	 */
	@Override
	public DbResponse getDictDetailListByDictcode(String dictCode, String tenantId) {

		List<SysDictVo> dataList = sysDictDao.selectListByDictCode(dictCode,tenantId);
		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,
				ResponseConstant.SELECT_SUCCESS_MSG,dataList);
	}


	/**
	 * @Author: Donny_dong
	 * @Description: 更新对应字段的状态
	 * @Param:
	 * @return:
	 * @Date: 2020/5/25
	 */
	@Override
	public DbResponse updateSysDict(SysDictDto sysDictDto) {
		try {
			SysDictVo sysDictVo = sysDictDao.selectByPrimaryKey(sysDictDto.getId());
			if (Objects.isNull(sysDictVo)){
				return new DbResponse(ResponseConstant.RES_FAIL_CODE,"该数据字典不存在");
			}
			SysDict sysDict = new SysDict();
			BeanUtils.copyProperties(sysDictDto,sysDict);
			sysDictDao.updateByPrimaryKeySelective(sysDict);
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,"修改成功！");
		} catch (Exception e) {
			log.error("修改数据字典状态异常："+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"修改数据字典状态异常："+e);
		}
	}

	/**
	 * @Author: Donny_dong
	 * @Description: 获取参数设置的字典列表
	 * @Param:
	 * @return:
	 * @Date: 2020/5/26
	 */
	@Override
	public DbResponse getParamSetList(String tenantId) {
		try {
			SysDictParamSetDto sysDictParamSetDto = new SysDictParamSetDto();
			List<SysDictVo> buttonPermissionList = sysDictDao.selectListByDictCode(ConstantCode.SysDictCode.BUTTON_PERMISSIONS, tenantId);
			List<SysDictVo> detailViewList = sysDictDao.selectListByDictCode(ConstantCode.SysDictCode.ORDER_DETAIL_ATTRIBUTE_VIEW, tenantId);
			ArrayList<NonSysDictParamDto> nonSysDictParams = new ArrayList<>();
			// 添加催单时间间隔
			nonSysDictParams.add(getUrgeTimeInterval());

			sysDictParamSetDto.setButtonPermissionList(buttonPermissionList);
			sysDictParamSetDto.setDetailViewList(detailViewList);
			sysDictParamSetDto.setNonSysDictParamList(nonSysDictParams);
			return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.SELECT_SUCCESS_MSG,sysDictParamSetDto);
		} catch (Exception e) {
			log.error("获取其他参数设置列表异常："+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE, "获取其他参数设置列表异常："+e);
		}
	}
	/**
	 * TODO 获取催单时间间隔信息
	 * @author: victor_tang
	 * @createtime: 2020/5/26 19:08
	 * @param
	 * @return: com.egoo.ews.service.pojo.dto.NonSysDictParam
	 */
	private NonSysDictParamDto getUrgeTimeInterval() {
		NonSysDictParamDto nonSysDictParam = new NonSysDictParamDto();
		nonSysDictParam.setCode(RedisKeyConstant.WORK_ORDER_URGE);
		nonSysDictParam.setValue(redisUtil.getStr(RedisKeyConstant.WORK_ORDER_URGE));
		nonSysDictParam.setName(ConstantCode.NonSysDictParam.URGE_TIME_INTERVAL_NAME);
		return nonSysDictParam;
	}

	/**
	 * @Author: Donny_dong
	 * @Description: 字典新增
	 * @Param:
	 * @return:
	 * @Date: 2020/11/26
	 */
	@Override
	public DbResponse add(SysDictDto sysDictDto) {
		try {
			List<SysDictChildrenDto> sysDictChildrenDtoList = sysDictDto.getSysDictChildrenDtoList();
			String tenantId = sysDictDto.getTenantId();
			ArrayList<SysDict> sysDicts = new ArrayList<>();
			for (SysDictChildrenDto s:sysDictChildrenDtoList
				 ) {
				SysDict sysDict = new SysDict();
				BeanUtils.copyProperties(s,sysDict);
				Date date = new Date();
				sysDict.setId(createCodeUtil.getUuid());
				sysDict.setCreateTime(date);
				sysDict.setUpdateTime(date);
				sysDict.setTenantId(tenantId);
				sysDicts.add(sysDict);

			}
			int in = sysDictDao.deleteCustomData(sysDictChildrenDtoList.get(0).getPid());

			int i = sysDictDao.insertBatch(sysDicts);
			log.info("字典数据新增结果"+i);
			if (i>0){
				return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.ADD_SUCCESS_MSG);
			}
		} catch (BeansException e) {
			e.printStackTrace();
			log.info("字典数据新增异常"+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,e.toString());
		}
		return new DbResponse(ResponseConstant.RES_FAIL_CODE,ResponseConstant.ADD_FAIL_MSG);
	}

	/**
	 * @Author: Donny_dong
	 * @Description: 删除字典信息
	 * @Param:
	 * @return:
	 * @Date: 2020/11/26
	 */
	@Override
	public DbResponse delete(SysDictDto sysDictDto) {
		try {
			List<String> ids = sysDictDto.getIds();
			int i = sysDictDao.deteleBatch(ids);
			if (i>0){
				return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,ResponseConstant.DEL_SUCCESS_MSG);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info("删除异常"+e);
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,e.toString());
		}
		return new DbResponse(ResponseConstant.RES_FAIL_CODE,ResponseConstant.DEL_FAIL_MSG);
	}
}
